import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

/**
 * 
import {fileURLToPath, URL} from 'node:url

'@': fileURLToPath(new URL('./src', import.meta.url))
'@': new URL('./src', import.meta.url).pathname

两种等效的配置方法

 */


// https://vitejs.dev/config/
export default defineConfig({
  base: '/main/',
  plugins: [vue(
    {
      template: {
        compilerOptions: {
          isCustomElement: tag => /^micro-app/.test(tag)
        }
      }
    }
  )],
  resolve: {
    alias: {
      '@': new URL('./src', import.meta.url).pathname
    }
  },
  css: {
    preprocessorOptions: {
      scss: {
        // https://www.cnblogs.com/crispyChicken/p/18420010
        // api: "modern-compiler", // or 'modern'
        additionalData: `@use "./src/styles/element/index.scss" as *;`
      }
    }
  },
  server: {
    // https://vite.dev/config/server-options
    proxy: {
      // string shorthand: http://localhost:5173/foo -> http://localhost:4567/foo
      '/foo': 'http://localhost:4567',
      // with options: http://localhost:5173/api/bar-> http://jsonplaceholder.typicode.com/bar
      '/api': {
        // target: 'http://jsonplaceholder.typicode.com',
        target: 'http://hrotest.huokesaas.com',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, ''),
      },
      // with RegExp: http://localhost:5173/fallback/ -> http://jsonplaceholder.typicode.com/
      '^/fallback/.*': {
        target: 'http://jsonplaceholder.typicode.com',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/fallback/, ''),
      },
      // Using the proxy instance
      // '/api': {
      //   target: 'http://jsonplaceholder.typicode.com',
      //   changeOrigin: true,
      //   configure: (proxy, options) => {
      //     // proxy will be an instance of 'http-proxy'
      //   },
      // },
      // Proxying websockets or socket.io: ws://localhost:5173/socket.io -> ws://localhost:5174/socket.io
      // Exercise caution using `rewriteWsOrigin` as it can leave the proxying open to CSRF attacks.
      '/socket.io': {
        target: 'ws://localhost:5174',
        ws: true,
        rewriteWsOrigin: true,
      }

    }
  }
})
